﻿using System.Drawing;
using System.IO;
using System;
using System.Windows.Forms;
using SFML.Graphics;
using Strom.MapEditor.Graphic;
using System.Diagnostics;

namespace Strom.MapEditor.GUI
{
    public partial class frmEditor : Form
    {
        private System.Threading.Timer _displayInfos;
        private Pen _selectedTilePen;

        public frmEditor(string accountName)
        {
            InitializeComponent();
            this.Text = "Editeur de maps (Connecté en tant que : " + accountName + ")";
            _selectedTilePen = new Pen(System.Drawing.Color.Red,1);
        }

        private void frmEditor_Shown(object sender, EventArgs e)
        {
            Var.RenderWin = new RenderWindow(this.picGame.Handle);
            Initializer.Load();
            Functions.ChangeTileset(0);
            _displayInfos = new System.Threading.Timer(DisplayStatusInfos, null, 0, 1000);
            GameLoop.Start();
        }

        private void DisplayStatusInfos(object o)
        {
            lblFPS.Text = "FPS: " + Var.FPS;
            Var.FPS = 0;
            toolStripStatusLabel2.Text = "RAM: " + Environment.WorkingSet/1000000 + " Mo";
        }

        private void frmEditor_Load(object sender, System.EventArgs e)
        {
            MapEditorManager.GUI.Editor = this;

            foreach (var file in Directory.GetFiles("Graphique\\Tiles", "*.png"))
            {
                lstTiles.Items.Add(file.Substring(file.LastIndexOf('\\') + 1));
            }
            Functions.ChangeTileset(0);
        }

        protected override void OnClosed(System.EventArgs e)
        {
            Initializer.Unload();
            MapEditorManager.GUI.Login.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Functions.ChangeTileset((byte)lstTiles.SelectedIndex);
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            if (lstTiles.Items.Count - 1 != lstTiles.SelectedIndex)
            {
                lstTiles.SelectedIndex++;
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            if (0 != lstTiles.SelectedIndex)
            {
                lstTiles.SelectedIndex--;
            }
        }

        private void picGame_Click(object sender, System.EventArgs e)
        {

        }

        private void toolStripStatusLabel1_Click(object sender, System.EventArgs e)
        {

        }

        private void toolStripStatusLabel2_Click(object sender, System.EventArgs e)
        {

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void picTiles_Click(object sender, EventArgs e)
        {

        }

        private void picGame_MouseMove(object sender, MouseEventArgs e)
        {
            Var.CursorX = (byte)(e.X / 32);
            Var.CursorY = (byte)(e.Y / 32);

            lblPosition.Text = ("X : " + Var.CursorX + " Y : " + Var.CursorY);

            if (Var.MouseIsDown)
            {
                Functions.PlaceTiles(0, Var.CurrentLayer);
                //if (Var.TilesHeight == 0 & Var.TilesWidth == 0)
                //{
                //    Functions.PlaceTiles(0, Var.CurrentLayer);
                //}
                //else
                //{
                //    for (byte x = 0; x == Var.TilesWidth; x++)
                //    {
                //        for (byte y = 0; y == Var.TilesHeight; y++)
                //        {
                //            if (Var.CursorX + x <= (int)(this.picGame.Width / 32))
                //            {
                //                if (Var.CursorY + y <= (int)(this.picGame.Height / 32))
                //                {
                //                    MessageBox.Show("lol");
                //                }
                //            }

                //        }
                //    }
                //}
            }
        }

        private void picGame_MouseDown(object sender, MouseEventArgs e)
        {
            Var.MouseIsDown = true;
            Functions.PlaceTiles(0, Var.CurrentLayer);
        }

        private void picGame_MouseUp(object sender, MouseEventArgs e)
        {
            Var.MouseIsDown = false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 0;
            toolStripButton1.Checked = true;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 1;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = true;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 2;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = true;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 3;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = true;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 4;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = true;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 5;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = true;
            toolStripButton7.Checked = false;
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            Var.CurrentLayer = 6;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton6.Checked = false;
            toolStripButton7.Checked = true;
        }

        private void picTiles_MouseMove(object sender, MouseEventArgs e)
        {
            //Var.tempTilesX = (byte)(e.Y / 32);
            //Var.tempTilesY = (byte)(e.Y / 32);
            //Var.TilesHeight = (byte)(Var.tempTilesY - Var.TilesY);
            //Var.TilesWidth = (byte)(Var.tempTilesX - Var.TilesX);
        }

        private void picTiles_MouseDown(object sender, MouseEventArgs e)
        {
            Var.TilesX = (byte)(e.X / 32);
            Var.TilesY = (byte)(e.Y / 32);
            picTiles.Invalidate();

            //Var.tempTilesX = (byte)(e.Y / 32);
            //Var.tempTilesY = (byte)(e.Y / 32);
        }

        private void picTiles_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(_selectedTilePen, Var.TilesX * 32, Var.TilesY * 32, 32, 32);
        }

    }
}
